package cn.rubintry.bug.hunting;


import android.app.Application;
import android.content.Context;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.utils.UMUtils;

import java.io.File;

import cn.rubintry.bug.hunting.common.exception.CustomExceptionHandler;
import cn.rubintry.bug.hunting.room.DbManager;
import cn.rubintry.bug.lib.network.http.CallAdapterType;
import cn.rubintry.bug.lib.network.http.NetApiManager;
import cn.rubintry.bug.lib.network.http.interceptor.NetWorkRetryInterceptor;
import cn.rubintry.bug.lib.umeng.UmengInitializer;
import cn.rubintry.nbui.core.NBUI;
import cn.rubintry.nbui.core.config.NBElasticPullConfig;

public class BugHuntingApp extends Application {


    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }


    @Override
    public void onCreate() {
        super.onCreate();
        NetApiManager.getInstance().addInterceptor(new NetWorkRetryInterceptor());
        NetApiManager.getInstance().setTimeout(60000);
        NetApiManager.getInstance().init(BuildConfig.BASE_URL, AppUtils.isAppDebug() , CallAdapterType.LiveData);


        NBUI.getInstance()
                .config(NBElasticPullConfig.class)
                .setElasticCoefficient(0.82f)
                .endConfig();

        File logDir = new File(PathUtils.getExternalAppDataPath());
        if(!logDir.exists()){
            logDir.mkdirs();
        }

        LogUtils.getConfig().setDir(logDir).setLog2FileSwitch(true).setFilePrefix("Log_").setFileExtension(".log");

        Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());

        DbManager.getInstance().init(this);

        //友盟日志开关
        UMConfigure.setLogEnabled(AppUtils.isAppDebug());
        UmengInitializer.preInit(this);
        boolean isMainProcess = UMUtils.isMainProgress(this);
        if(isMainProcess && UmengInitializer.isAgree()){
            UmengInitializer.initialize(this);
        }

    }
}
